package org.test



import org.junit.*

import grails.test.mixin.*

@TestFor(PriceAssessmentController)
@Mock(PriceAssessment)
class PriceAssessmentControllerTests {
	
	Product targetProduct = new Product(
		barcode: "1234567", description: "A testing Product");

    def populateValidParams(params) {
      assert params != null
	  params["store"] = "Bed, Bath and Beyond"
	  params["price"] = 3.4
	  params["notes"] = "The product was on sale"
	  params["product"] = targetProduct
    }

    void testIndex() {
        controller.index()
        assert "/priceAssessment/list" == response.redirectedUrl
    }

    void testList() {

        def model = controller.list()

        assert model.priceAssessmentInstanceList.size() == 0
        assert model.priceAssessmentInstanceTotal == 0
    }
	
    void testCreate() {
       def model = controller.create()

       assert model.priceAssessmentInstance != null
    }

    void testSave() {
        controller.save()

        assert model.priceAssessmentInstance != null
        assert view == '/priceAssessment/create'

        response.reset()

        populateValidParams(params)
        controller.save()

        assert response.redirectedUrl == '/priceAssessment/show/1'
        assert controller.flash.message != null
        assert PriceAssessment.count() == 1
    }

    void testShow() {
        controller.show()

        assert flash.message != null
        assert response.redirectedUrl == '/priceAssessment/list'


        populateValidParams(params)
        def priceAssessment = new PriceAssessment(params)

        assert priceAssessment.save() != null

        params.id = priceAssessment.id

        def model = controller.show()

        assert model.priceAssessmentInstance == priceAssessment
    }

    void testEdit() {
        controller.edit()

        assert flash.message != null
        assert response.redirectedUrl == '/priceAssessment/list'


        populateValidParams(params)
        def priceAssessment = new PriceAssessment(params)

        assert priceAssessment.save() != null

        params.id = priceAssessment.id

        def model = controller.edit()

        assert model.priceAssessmentInstance == priceAssessment
    }

    void testUpdate() {
        controller.update()

        assert flash.message != null
        assert response.redirectedUrl == '/priceAssessment/list'

        response.reset()


        populateValidParams(params)
        def priceAssessment = new PriceAssessment(params)

        assert priceAssessment.save() != null

        // test invalid parameters in update
        params.id = priceAssessment.id
		params["product"] = null
        controller.update()

        assert view == "/priceAssessment/edit"
        assert model.priceAssessmentInstance != null

        priceAssessment.clearErrors()

        populateValidParams(params)
        controller.update()

        assert response.redirectedUrl == "/priceAssessment/show/$priceAssessment.id"
        assert flash.message != null

        //test outdated version number
        response.reset()
        priceAssessment.clearErrors()

        populateValidParams(params)
        params.id = priceAssessment.id
        params.version = -1
        controller.update()

        assert view == "/priceAssessment/edit"
        assert model.priceAssessmentInstance != null
        assert model.priceAssessmentInstance.errors.getFieldError('version')
        assert flash.message != null
    }

    void testDelete() {
        controller.delete()
        assert flash.message != null
        assert response.redirectedUrl == '/priceAssessment/list'

        response.reset()

        populateValidParams(params)
        def priceAssessment = new PriceAssessment(params)

        assert priceAssessment.save() != null
        assert PriceAssessment.count() == 1

        params.id = priceAssessment.id

        controller.delete()

        assert PriceAssessment.count() == 0
        assert PriceAssessment.get(priceAssessment.id) == null
        assert response.redirectedUrl == '/priceAssessment/list'
    }
}
